package com.itextpdf.text.pdf;

import com.itextpdf.text.*;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.net.MalformedURLException;

/**
 * Created by zc on 2017/3/16.
 */
public class HeaderFooter  extends PdfPageEventHelper {
    private String header = "";
    private String image = "";
    private int presentFontSize = 12;
    private Rectangle pageSize;
    private PdfTemplate total;
    private BaseFont bf;
    private Font fontDetail;

    public HeaderFooter() {
        this.pageSize = PageSize.A4;
        this.bf = null;
        this.fontDetail = null;
    }

    public HeaderFooter(String header, String image, int presentFontSize) {
        this.pageSize = PageSize.A4;
        this.bf = null;
        this.fontDetail = null;
        if(header != null) {
            this.header = header;
        }

        if(image != null) {
            this.image = image;
        }

        this.presentFontSize = presentFontSize;
    }

    public void onOpenDocument(PdfWriter writer, Document document) {
        this.total = writer.getDirectContent().createTemplate(50.0F, 50.0F);
    }

    public void onEndPage(PdfWriter writer, Document document) {
        try {
            if(this.bf == null) {
                this.bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
            }

            if(this.fontDetail == null) {
                this.fontDetail = new Font(this.bf, (float)this.presentFontSize, 0, BaseColor.BLUE);
            }
        } catch (DocumentException var16) {
            var16.printStackTrace();
        } catch (IOException var17) {
            var17.printStackTrace();
        }

        PdfContentByte cb = writer.getDirectContent();
        byte imageWidth = 50;
        byte imageHeight = 80;
        Image headImage = null;

        try {
            if(StringUtils.isNotBlank(this.image)) {
                headImage = Image.getInstance(this.image);
                headImage.setAbsolutePosition(document.left(), document.getPageSize().getTop() - 30.0F);
                headImage.scaleToFit((float)imageWidth, (float)imageHeight);
                cb.addImage(headImage);
            }
        } catch (BadElementException var12) {
            var12.printStackTrace();
        } catch (MalformedURLException var13) {
            var13.printStackTrace();
        } catch (IOException var14) {
            var14.printStackTrace();
        } catch (DocumentException var15) {
            var15.printStackTrace();
        }

        if(StringUtils.isBlank(this.header)) {
            this.header = "";
        }

        float headerLength = this.bf.getWidthPoint(this.header, (float)this.presentFontSize);
        ColumnText.showTextAligned(writer.getDirectContent(), 0, new Phrase(this.header, this.fontDetail), (document.rightMargin() + document.right() + document.leftMargin() - document.left() - headerLength) / 2.0F + 20.0F, document.top() + 10.0F, 0.0F);
        int pageS = writer.getPageNumber();
        String foot1 = "第 " + pageS + " 页 /共";
        Phrase footer = new Phrase(foot1, this.fontDetail);
        float len = this.bf.getWidthPoint(foot1, (float)this.presentFontSize);
        ColumnText.showTextAligned(cb, 1, footer, (document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20.0F, document.bottom() + 5.0F, 0.0F);
        cb.addTemplate(this.total, (document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20.0F, document.bottom() + 5.0F);
    }

    public void onCloseDocument(PdfWriter writer, Document document) {
        this.total.beginText();
        this.total.setFontAndSize(this.bf, (float)this.presentFontSize);
        this.total.setColorFill(BaseColor.BLUE);
        String foot2 = " " + (writer.getPageNumber() - 1) + " 页";
        this.total.showText(foot2);
        this.total.endText();
        this.total.closePath();
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setPresentFontSize(int presentFontSize) {
        this.presentFontSize = presentFontSize;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void setPageSize(Rectangle pageSize) {
        this.pageSize = pageSize;
    }

    public void setFontDetail(Font fontDetail) {
        this.fontDetail = fontDetail;
    }

    public void setBf(BaseFont bf) {
        this.bf = bf;
    }
}
